package com.atguigu.tingshu.order.api;

import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.hibernate.validator.constraints.br.TituloEleitoral;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Tag(name = "订单管理")
@RestController
@RequestMapping("api/order")
@SuppressWarnings({"all"})
public class OrderInfoApiController {

    @Autowired
    private OrderInfoService orderInfoService;


    /**
     * 订单确认页面数据渲染-展示“商品”结算页
     *
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "订单确认页面数据渲染-展示“商品”结算页")
    @PostMapping("/orderInfo/trade")
    public Result<OrderInfoVo> orderTrade(@RequestBody TradeVo tradeVo) {
        //1.获取用户ID
        Long userId = AuthContextHolder.getUserId();
        //2.调用业务逻辑层处理订单确认数据汇总
        OrderInfoVo orderInfoVo = orderInfoService.orderTrade(userId, tradeVo);


        return Result.ok(orderInfoVo);
    }


    /**
     * 提交订单（包含余额付款） 如果是余额付款则进行扣费；反之则只需要保存订单即可
     *
     * @param orderInfoVo
     * @return 新增后订单编号，1.根据订单编号查询订单详情  2.跟在线支付平台对接
     */
    @GuiGuLogin
    @Operation(summary = "提交订单（包含余额付款）")
    @PostMapping("/orderInfo/submitOrder")
    public Result<Map<String, String>> submitOrder(@RequestBody @Validated OrderInfoVo orderInfoVo) {
        Long userId = AuthContextHolder.getUserId();
        String orderNo = orderInfoService.submitOrder(userId, orderInfoVo);
        Map<String, String> map = new HashMap<>();
        map.put("orderNo", orderNo);
        return Result.ok(map);
    }

    /**
     * 根据订单编号查询订单详情(订单商品明细、订单优惠明细)
     *
     * @param orderNo
     * @return
     */
    @Operation(summary = "根据订单编号查询订单详情(订单商品明细、订单优惠明细)")
    @GetMapping("/orderInfo/getOrderInfo/{orderNo}")
    public Result<OrderInfo> getOrderInfo(@PathVariable String orderNo) {
        OrderInfo orderInfo = orderInfoService.getOrderInfo(orderNo);
        return Result.ok(orderInfo);
    }


    /**
     * 分页获取当前用户订单列表
     * @param page
     * @param limit
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "分页获取当前用户订单列表")
    @GetMapping("/orderInfo/findUserPage/{page}/{limit}")
    public Result<Page<OrderInfo>> getUserOrderByPge(@PathVariable Integer page, @PathVariable Integer limit){
        //1.获取登录用户ID
        Long userId = AuthContextHolder.getUserId();
        //2.创建分页对象（页码、页大小）
        Page<OrderInfo> pageInfo = new Page<>(page, limit);
        //3.调用业务层获取订单分页
        orderInfoService.getUserOrderByPge(pageInfo, userId);
        return Result.ok(pageInfo);
    }


    /**
     * 用户在线付款成功后，修改订单支付状态以及新增购买记录
     * @param orderNo
     * @return
     */
    @Operation(summary = "用户在线付款成功后，修改订单支付状态以及新增购买记录")
    @GetMapping("/orderInfo/orderPaySuccess/{orderNo}")
    public Result orderPaySuccess(@PathVariable String orderNo){
        orderInfoService.orderPaySuccess(orderNo);
        return Result.ok();
    }

}

